package com.whiteape.book.ui.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.KeyEvent;

import androidx.appcompat.widget.Toolbar;

import com.whiteape.book.R;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.ui.base.BaseActivity;
import com.whiteape.book.ui.fragment.MediaGroupDetailFragment;

import java.util.ArrayList;

/**
 * 书架分组详情
 */
public class MediaGroupActivity extends BaseActivity {

    ArrayList<CollectionMediaBean> mData;
    int mediaType;

    MediaGroupDetailFragment mediaGroupFragment;
    @Override
    protected void initView() {
        super.initView();
        mediaGroupFragment = MediaGroupDetailFragment.newInstance(mediaType, mData);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.ll_download_content, mediaGroupFragment)
                .commit();
    }

    @Override
    protected int getContentId() {
        return R.layout.activity_media_group;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if(savedInstanceState!= null && savedInstanceState.containsKey("media_list")){
            mData = savedInstanceState.getParcelableArrayList("media_list");
            mediaType = savedInstanceState.getInt("media_type");
        }else {
            mData = getIntent().getParcelableArrayListExtra("media_list");
            mediaType = getIntent().getIntExtra("media_type", MediaType.MEDIA_TYPE_NOVEL);
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putParcelableArrayList("media_list", mData);
    }

    @Override
    protected void setUpToolbar(Toolbar toolbar) {
        super.setUpToolbar(toolbar);
        String title = getIntent().getStringExtra("group_name");
        if (null != getSupportActionBar()) {
            getSupportActionBar().setTitle(title);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mediaGroupFragment != null){
            if(mediaGroupFragment.onKeyDown(keyCode, event)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
